/**
 * 
 */
package controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.functors.WhileClosure;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sun.xml.internal.bind.v2.model.core.ID;

import daos.StudentClassDAO;
import daos.StudentProfileDAO;

import model.Course;
import model.StudentClass;
import model.StudentProfile;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author JK
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class CreateStudentClassesController {
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private int MIN_CLASS_SIZE;
	//private int MAX_REGULAR_CLASS_SIZE=7;
	
	private ApplicationContext applicationContext;
	public List<StudentClass> createStudentClasses(int MIN_CLASS_SIZE) {
		// begin-user-code
		this.MIN_CLASS_SIZE=MIN_CLASS_SIZE;
	applicationContext = new ClassPathXmlApplicationContext(new String[] {"classpath:persistenceTest/applicationContext.xml"});
		
		StudentProfileDAO	studentProfileDAO  = (StudentProfileDAO)applicationContext.getBean("studentProfileDAO");
	List<StudentProfile> studentProfiles=studentProfileDAO.findStudents();
	Collections.sort(studentProfiles);
	
			return createStudentGroupByPreferenciesGpa(studentProfiles);
		

		// end-user-code
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public List<StudentClass> createStudentGroupByPreferenciesGpa(List<StudentProfile> studentProfiles) {
		List<StudentClass> classes = new ArrayList<StudentClass>();
		List<StudentProfile>  sProfilesTemp=new ArrayList<StudentProfile>();
		StudentClassDAO studentClassDAO=  (StudentClassDAO)applicationContext.getBean("studentClassDAO");
		int classCount=0;
		if(studentProfiles.size()<MIN_CLASS_SIZE)
		{
			return null; 
		}
		for (int i=studentProfiles.size()-1;i>=0;i--) {
			sProfilesTemp.add(studentProfiles.remove(i));
			if(sProfilesTemp.size()==MIN_CLASS_SIZE){
				
				createClass(sProfilesTemp,classCount);
				classes.add(studentClassDAO.getClassByName("Class_"+classCount));
				classCount++;
				sProfilesTemp.clear();
			}
	   	
			
		}
		   if(sProfilesTemp.size()>0)
	      	{//assign the remaining  student  the last class 
	    		
	    	StudentClass  lastClass=studentClassDAO.getClassByName("Class_"+(--classCount));
	    	List<StudentProfile> remainingStudentsList = new ArrayList<StudentProfile>();
	    	remainingStudentsList=lastClass.getStudents();
	    	for (StudentProfile studentProfile : sProfilesTemp) {
	    		
	    		remainingStudentsList.add(studentProfile);
			}
	    	lastClass.setStudents(remainingStudentsList);
	    	studentClassDAO.save(lastClass);
	    	classes.add(lastClass);
	      	}	
		
		return classes;
		// end-user-code
	}

	
	public void createClass(List <StudentProfile> studentProfiles,int classCount) {
		// begin-user-code
StudentClass studentClass=new StudentClass("Class_"+classCount);
		studentClass.setStudents(studentProfiles);
		StudentClassDAO studentClassDAO=  (StudentClassDAO)applicationContext.getBean("studentClassDAO");
	studentClassDAO.save(studentClass);
		// end-user-code
	}


	
}